/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package docmanager.business.objects;


import java.sql.*;
import java.util.*;
import bf.*;

// Debiera estar en un paquete de objetos de negocio.

/**
 *
 * @author Juan Lucas
 */
public class Usuario extends BusinessObject
{
    private String login;
    private String clave;

   // private int id_rol;
    
    List roles;

    public Usuario(int id, String login, String clave)
    {
        this.id = id;
        this.login = login;
        this.clave = clave;
    }
    
    public Usuario(String login, String clave)
    {
        this.login = login;
        this.clave = clave;
    }

    public Usuario(Connection conn, int id, String login, String clave)
    {
        this.id = id;
        this.login = login;
        this.clave = clave;
        this.conn = conn;
    }

    
   
   
    public Usuario(Connection conn, String login) throws SQLException
    {
      String query = "SELECT id,login,clave FROM usuarios WHERE login='"+login+"'";
      ResultSet rs = DataAccess.selectQueryStatic(conn, query);
      rs.next();
      this.id = rs.getInt("id");
      this.login = rs.getString("login");
      this.clave = rs.getString("clave");
      this.conn = conn;
      //this.id_rol = rs.getInt("id_rol");
    }

    
    
/**
     * Recibe una conexion y la deja abierta. Sirve para transacciones.
     * @param id
     * @param conn
     * @throws SQLException 
     */    
    public Usuario( Connection conn, int id) throws SQLException
    {
      String query = "SELECT id,login,clave FROM usuarios WHERE id='"+id+"'";
      ResultSet rs = DataAccess.selectQueryStatic(conn, query);
      rs.next();
      this.id = rs.getInt("id");
      this.login = rs.getString("login");
      this.clave = rs.getString("clave");  
      this.conn = conn;
    }
    
    public String getClave()
    {
        return clave;
    }

    public void setClave(String clave)
    {
        this.clave = clave;
    }

    public String getLogin()
    {
        return login;
    }

    public void setLogin(String login)
    {
        this.login = login;
    }

        
   
    public void guardarNuevo(Connection conn) throws SQLException
    {
        String query = "INSERT INTO usuarios (login, clave) VALUES('" + login
                + "','" + clave +"')";
        DataAccess.dmlQueryStatic(conn,query);        
    }
    
    public void modificar(Connection conn) throws SQLException
    {
       String query = "UPDATE usuarios "
               + "SET login='" + this.getLogin()+ "', " +"clave='"+clave+"' "
               + "WHERE id=" + this.getId();
       DataAccess.dmlQueryStatic(conn,query);        
    }
    
    
    public List cargarRoles() throws SQLException
    {
        String query = "SELECT roles.id as id, roles.nombre as nombre, roles.complete_control as cc "
                + "FROM usuarios, roles, usuarios_roles "
                + "WHERE usuarios.id = usuarios_roles.id_usuario and roles.id = usuarios_roles.id_rol and usuarios.id = " +this.getId();
          List l = new LinkedList();
          System.out.println("QUERY: " + query);
          ResultSet rs = DataAccess.selectQueryStatic(conn, query);
          while (rs.next())
          {
            int id = rs.getInt("id");
            String nombre = rs.getString("nombre");
            int temp = rs.getInt("cc");
            boolean completeControl;
            if (temp == 1)
                completeControl = true;
            else
                completeControl = false;
            l.add(new Rol(conn, id,nombre,completeControl));
      }
      this.roles = l;
      return(l);
    }
    
    public String getDescRoles() throws SQLException
    {
        if (this.roles == null)
            this.roles = this.cargarRoles();
        return Rol.listarRoles(this.roles);
    }
    
    public Set obtenerIdRoles() throws SQLException
    {
        if (this.roles == null)
            this.roles = this.cargarRoles();
        Set s = new HashSet();
        Iterator it = this.roles.iterator();
        while (it.hasNext())
        {
            Rol r = (Rol) it.next();
            s.add(new Integer(r.getId()));
        }
        return s;
    }
    
    
    public static boolean existeUsuario(Connection conn, String login)
            throws SQLException
    {
       String query = "SELECT id from usuarios where login='"+login+"'";
       return DataAccess.existenceQueryStatic(conn, query);
    }
    
    
    
    public static List obtenerTodosUsuarios(Connection conn) throws SQLException
    {
      String query = "SELECT id, login, clave FROM usuarios";
      List l = new LinkedList();
      ResultSet rs = DataAccess.selectQueryStatic(conn, query);
      while (rs.next())
      {
          int id = rs.getInt("id");;
          String login = rs.getString("login");
          String clave = rs.getString("clave");
          l.add(new Usuario(conn,id,login,clave));
      }
      return(l);
    }
 
    /**
     * Devuelve todos los usuarios que no son Sysop.
     * @param conn
     * @return
     * @throws SQLException
     */
    
    public static List obtenerUsuariosComunes(Connection conn) throws SQLException
    {
      String query = "SELECT id, login, clave FROM usuarios";
      List l = new LinkedList();
      ResultSet rs = DataAccess.selectQueryStatic(conn, query);
      while (rs.next())
      {
          int id = rs.getInt("id");;
          String login = rs.getString("login");
          String clave = rs.getString("clave");
          Usuario u =new Usuario(conn,id,login,clave);
          u.cargarRoles();
          if (!u.esSysAdmin())
        	  l.add(u);
      }
      return(l);
    }
 
    
    
    // Auxiliar.

    /*
    public boolean esOpeario()
    {
    	return this.getId()== Rol.OPERARIO;
    }*/
    
    public boolean esTipoRol(int idRol)
    throws SQLException
    {
    	if (this.roles == null)
    		this.cargarRoles();
    	Iterator it = this.roles.iterator();
    	while (it.hasNext())
    	{
    		Rol rol = (Rol) it.next();
    		if (rol.getId() == idRol)
    			return true;
    	}
    	return false;
    }
    
    public boolean esSysAdmin()
    throws SQLException
    {
    	return this.esTipoRol(Rol.SYSOP);
    }
    
    public boolean esIntermedio()
    throws SQLException
    {
    	return this.esTipoRol(Rol.JEFE_SECTOR) || this.esTipoRol(Rol.ADMINISTRATIVO) || this.esTipoRol(Rol.SUPERVISOR);
    }
    
    public boolean esOperario()
    throws SQLException
    {
    	return this.esTipoRol(Rol.OPERARIO);
    }
    
    public boolean esCGC()
    throws SQLException
    {
    	return this.esTipoRol(Rol.CGC);
    }


}
